/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia.JPAControllers;

import Logica.Core.MateriaPrima.YerbaPropia;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Logica.Core.Plantaciones.Parcela;
import Persistencia.JPAControllers.exceptions.NonexistentEntityException;
import Persistencia.JPAControllers.exceptions.PreexistingEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author DANIEL
 */
public class YerbaPropiaJpaController implements Serializable {

    public YerbaPropiaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public YerbaPropiaJpaController() {
        emf = Persistence.createEntityManagerFactory("SecaderoPU");
    }

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(YerbaPropia yerbaPropia) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Parcela miParcela = yerbaPropia.getMiParcela();
            if (miParcela != null) {
                miParcela = em.getReference(miParcela.getClass(), miParcela.getNro());
                yerbaPropia.setMiParcela(miParcela);
            }
            em.persist(yerbaPropia);
            if (miParcela != null) {
                miParcela.getMisYerbas().add(yerbaPropia);
                miParcela = em.merge(miParcela);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findYerbaPropia(yerbaPropia.getId()) != null) {
                throw new PreexistingEntityException("YerbaPropia " + yerbaPropia + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(YerbaPropia yerbaPropia) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            YerbaPropia persistentYerbaPropia = em.find(YerbaPropia.class, yerbaPropia.getId());
            Parcela miParcelaOld = persistentYerbaPropia.getMiParcela();
            Parcela miParcelaNew = yerbaPropia.getMiParcela();
            if (miParcelaNew != null) {
                miParcelaNew = em.getReference(miParcelaNew.getClass(), miParcelaNew.getNro());
                yerbaPropia.setMiParcela(miParcelaNew);
            }
            yerbaPropia = em.merge(yerbaPropia);
            if (miParcelaOld != null && !miParcelaOld.equals(miParcelaNew)) {
                miParcelaOld.getMisYerbas().remove(yerbaPropia);
                miParcelaOld = em.merge(miParcelaOld);
            }
            if (miParcelaNew != null && !miParcelaNew.equals(miParcelaOld)) {
                miParcelaNew.getMisYerbas().add(yerbaPropia);
                miParcelaNew = em.merge(miParcelaNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                int id = yerbaPropia.getId();
                if (findYerbaPropia(id) == null) {
                    throw new NonexistentEntityException("The yerbaPropia with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(int id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            YerbaPropia yerbaPropia;
            try {
                yerbaPropia = em.getReference(YerbaPropia.class, id);
                yerbaPropia.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The yerbaPropia with id " + id + " no longer exists.", enfe);
            }
            Parcela miParcela = yerbaPropia.getMiParcela();
            if (miParcela != null) {
                miParcela.getMisYerbas().remove(yerbaPropia);
                miParcela = em.merge(miParcela);
            }
            em.remove(yerbaPropia);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<YerbaPropia> findYerbaPropiaEntities() {
        return findYerbaPropiaEntities(true, -1, -1);
    }

    public List<YerbaPropia> findYerbaPropiaEntities(int maxResults, int firstResult) {
        return findYerbaPropiaEntities(false, maxResults, firstResult);
    }

    private List<YerbaPropia> findYerbaPropiaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from YerbaPropia as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public YerbaPropia findYerbaPropia(int id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(YerbaPropia.class, id);
        } finally {
            em.close();
        }
    }

    public int getYerbaPropiaCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from YerbaPropia as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
